// node fenbao.js  
// npm cache verify  
// npm cache clean --force   


const fs = require('fs-extra');


// 下边可以优化为通过正则，直接复制从微信小程序端复制过来的代码
let pages_api_file = [
  "main-api-action-sheet",
  "main-api-add-eventlistener",
  "main-api-ajax",
  "main-api-alert",
  "main-api-app-version",
  "main-api-cancel-ajax",
  "main-api-cancel-download",
  "main-api-close-to-win",
  "main-api-close-win",
  "main-api-confirm",
  "main-api-download",
  "main-api-get-globaldata",
  "main-api-get-picture",
  "main-api-get-prefs",
  "main-api-hide-progress",
  "main-api-open-picker",
  "main-api-open-tab-layout",
  "main-api-open-win",
  "main-api-page-param",
  "main-api-remove-eventlistener",
  "main-api-remove-prefs",
  "main-api-safe-area",
  "main-api-sample-page",
  "main-api-save-media-toalbum",
  "main-api-send-event",
  "main-api-set-globaldata",
  "main-api-set-navbar-attr",
  "main-api-set-prefs",
  "main-api-set-tab-layoutattr",
  "main-api-set-tabbar-attr",
  "main-api-show-progress",
  "main-api-toast",
  "main-api-win-height",
  "main-api-win-width",
  "main-api-windows"
]
let pages_avm_demo_file = [
  "demo-action-sheet",
  "demo-badge",
  "demo-button",
  "demo-card",
  "demo-checkbox",
  "demo-checklist",
  "demo-collapse",
  "demo-desense-text",
  "demo-divider",
  "demo-dropdown",
  "demo-empty",
  "demo-error-block",
  "demo-floating-panel",
  "demo-grid",
  "demo-icon",
  "demo-image",
  "demo-image-uploader",
  "demo-image-viewer",
  "demo-infinite-scroll",
  "demo-input",
  "demo-list",
  "demo-loading",
  "demo-mask",
  "demo-nav-bar",
  "demo-notice-bar",
  "demo-number-keyboard",
  "demo-page-indicator",
  "demo-popover",
  "demo-popup",
  "demo-progress-bar",
  "demo-radio",
  "demo-rate",
  "demo-result",
  "demo-tab-bar",
  "demo-tabs",
  "demo-tag",
  "demo-textarea",
  "demo-tree-select",
  "demo-water-mark"
]
let pages_avm_demo_two_file = [
  "demo-search",
  "demo-selector",
  "demo-side-bar",
  "demo-space",
  "demo-stepper",
  "demo-steps",
  "demo-swiper",
  "demo-switch"
]
let pages_template_file = [
  "page-template-about-info-page",
  "page-template-busniess-paid-page",
  "page-template-bussiness-index",
  "page-template-company-show-index",
  "page-template-eduandtrain-lesson-page",
  "page-template-eduandtrain-order-page",
  "page-template-education-detail-page",
  "page-template-education-index",
  "page-template-education-user-page",
  "page-template-find-back-password",
  "page-template-goods-detail-page",
  "page-template-instance-list-page",
  "page-template-login-page",
  "page-template-pintuan-classify-page",
  "page-template-pintuan-detail-page",
  "page-template-pintuan-index",
  "page-template-pintuan-pay-page",
  "page-template-pintuan-start-page",
  "page-template-pintuan-user-page",
  "page-template-register",
  "page-template-shop-list"
]
let willClearedDir = [
  "avm-base-button",
  "avm-base-camera",
  "avm-base-checkbox",
  "avm-base-checkbox-group",
  "avm-base-form",
  "avm-base-frame",
  "avm-base-frame-group",
  "avm-base-grid-view",
  "avm-base-image",
  "avm-base-input",
  "avm-base-list-view",
  "avm-base-picker",
  "avm-base-progress",
  "avm-base-radio",
  "avm-base-radio-group",
  "avm-base-refresh",
  "avm-base-rich-text",
  "avm-base-scroll-view",
  "avm-base-slider",
  "avm-base-swiper",
  "avm-base-switch",
  "avm-base-text",
  "avm-base-textarea",
  "avm-base-video",
  "avm-base-view",
  "template-es",
  "template-es-case",
  "template-es-detail",
  "template-es-join",
  "template-es-main",
  "template-et",
  "template-et-course",
  "template-et-course-detail",
  "template-et-course-list",
  "template-et-course-pay",
  "template-et-course-preorder",
  "template-et-home",
  "template-et-order-detail",
  "template-et-order-list",
  "template-et-pay-result",
  "template-et-play-video",
  "template-et-preorder-detail",
  "template-et-preorder-list",
  "template-et-user",
  "template-fre",
  "template-fre-about",
  "template-fre-account",
  "template-fre-address",
  "template-fre-balancehelp",
  "template-fre-cityselector",
  "template-fre-coupon",
  "template-fre-customerservice",
  "template-fre-message",
  "template-fre-myorder",
  "template-fre-order",
  "template-fre-personalcenter",
  "template-fre-setaddress",
  "template-fre-setting",
  "template-fre-shoppingcart",
  "template-fre-ware",
  "template-od",
  "template-od-cart",
  "template-od-detail",
  "template-od-main",
  "template-od-order_list",
  "template-od-place_order",
  "template-od-shopping_cart",
  "template-od-sort",
  "template-od-user",
  "template-of",
  "template-of-goods_add",
  "template-of-goods_detail",
  "template-of-main_cart",
  "template-of-main_home",
  "template-of-main_menu",
  "template-of-main_user",
  "template-of-pay_result",
  "template-of-pending_order",
  "template-sgm",
  "template-sgm-about",
  "template-sgm-address_list",
  "template-sgm-commodity_detail",
  "template-sgm-edit_address",
  "template-sgm-main",
  "template-sgm-order",
  "template-sgm-order_detail",
  "template-sgm-pay",
  "template-sgm-personal_data",
  "template-sgm-sort",
  "template-sgm-sort_list",

  "app-api-set-full-screen",
  "app-api-bring-frame-to-front",
  "app-api-access-native",
  "app-api-agreed-privacy",
  "app-api-animation",
  "app-api-app-id",
  "app-api-app-installed",
  "app-api-app-name",
  "app-api-app-param",
  "app-api-apply-Certificates",
  "app-api-box-dir",
  "papp-api-bring-frame-to-front",
  "app-api-cache-dir",
  "app-api-call",
  "app-api-cancel-notification",
  "app-api-channel",
  "app-api-clear-cache",
  "app-api-close-drawer-pane-fixed",
  "app-api-close-drawer-pane-slid",
  "app-api-close-drawer-pane",
  "app-api-close-frame",
  "app-api-close-frame-group",
  "app-api-close-popover-win",
  "app-api-close-slid-pane-fixed",
  "app-api-close-slid-pane-slide",
  "app-api-close-slid-pane",
  "app-api-close-widget",
  "app-api-connection-type",
  "app-api-debug",
  "app-api-device-id",
  "app-api-device-model",
  "app-api-device-name",
  "app-api-device-token",
  "app-api-exec-script-demo",
  "app-api-exec-script",
  "app-api-frame-height",
  "app-api-frame-name",
  "app-api-frame-width",
  "app-api-frames",
  "app-api-fs-dir",
  "app-api-full-screen",
  "app-api-get-cache-size",
  "app-api-get-free-disk-space",
  "app-api-get-interface-style",
  "app-api-get-location",
  "app-api-get-navbar-attr-demo",
  "app-api-get-navbar-attr",
  "app-api-get-phone-number",
  "app-api-get-total-space",
  "app-api-has-permission",
  "app-api-history-back",
  "app-api-history-forward",
  "app-api-image-Cache",
  "app-api-install-app",
  "app-api-is-recovery-mode",
  "app-api-jailbreak",
  "app-api-language",
  "app-api-load-data",
  "app-api-load-secure-value",
  "app-api-lock-slid-pane-fixed",
  "app-api-lock-slid-pane-slid",
  "app-api-lock-slid-pane",
  "app-api-mail",
  "app-api-notification",
  "app-api-open-app",
  "app-api-open-contacts",
  "app-api-open-drawer-layout-fixed",
  "app-api-open-drawer-layout-slid",
  "app-api-open-drawer-layout",
  "app-api-open-drawer-pane-fixed",
  "app-api-open-drawer-pane-slid",
  "app-api-open-drawer-pane",
  "app-api-open-frame",
  "app-api-open-frame-group",
  "app-api-open-picker",
  "app-api-open-popover-win",
  "app-api-open-slid-layout-ifleft",
  "app-api-open-slid-layout-original",
  "app-api-open-slid-layout",
  "app-api-open-slid-pane-fixed",
  "app-api-open-slid-pane-slid",
  "app-api-open-slid-pane",
  "app-api-open-video",
  "app-api-open-widget",
  "app-api-operator",
  "app-api-page-down",
  "app-api-page-up",
  "app-api-parse-tapmode",
  "app-api-pause-play",
  "app-api-platform",
  "app-api-prompt",
  "app-api-read-file",
  "app-api-reboot-app",
  "app-api-refresh-header-load-done",
  "refresh-header",
  "app-api-refresh-header-loading",
  "refresh-header",
  "app-api-remove-launch-view-demo-video",
  "app-api-remove-launch-view",
  "app-api-request-permission",
  "app-api-require",
  "app-api-screen-capture",
  "app-api-screen-height",
  "app-api-screen-width",
  "app-api-send-frame-to-back",
  "app-api-set-app-icon-badge",
  "app-api-set-blur-effect",
  "app-api-set-custom-refresh-header-info",
  "app-api-set-focus",
  "app-api-set-frame-attr",
  "app-api-set-frame-client",
  "app-api-set-frame-group-attr",
  "app-api-set-frame-group-index",
  "papp-api-set-full-screen",
  "app-api-set-interface-style",
  "app-api-set-keep-screen-on",
  "app-api-set-menu-items",
  "app-api-set-refresh-header-info",
  "refresh-header",
  "app-api-set-screen-orientation",
  "app-api-set-screen-secure",
  "app-api-set-status-bar-style",
  "app-api-set-Tabbar-Item-Attr",
  "app-api-set-win-attr",
  "app-api-show-float-box",
  "app-api-show-launch-view-demo-video",
  "app-api-show-launch-view",
  "app-api-sms",
  "app-api-start-location",
  "app-api-start-play",
  "app-api-start-record",
  "app-api-start-sensor",
  "app-api-status-bar-appearance",
  "app-api-stop-location",
  "app-api-stop-play",
  "app-api-stop-record",
  "app-api-stop-sensor",
  "app-api-system-type",
  "app-api-system-version",
  "app-api-to-launcher",
  "app-api-ui-mode",
  "app-api-uninstall-app",
  "app-api-unlock-slid-pane-fixed",
  "app-api-unlock-slid-pane-slid",
  "app-api-unlock-slid-pane",
  "app-api-version",
  "app-api-wgt-param",
  "app-api-wgt-root-dir",
  "app-api-win-name",
  "app-api-write-file",
]



const config = {
  // rootPath: `../ceshi`,
  // oldFile: '../ceshi/pages/'
  rootPath: `../AVM-UI/@widget/out-mp`,
  oldFile: '../AVM-UI/@widget/out-mp/pages/'

  
}

// `[1/5 创建分包所需要的4个文件夹]`);
if (1) {
  console.log(`[1/5 创建分包所需要的4个文件夹]`);
  // 创建pages_api 目录
  for (let i = 0; i < pages_api_file.length; i++) {
    fs.mkdirsSync(`${config.rootPath}/pages-api/${pages_api_file[i]}`, function (error) {
      if (error) {
        console.log(error);
      }
      console.log(`${pages_api_file[i]} 创建成功`);
    })
  }


  // 创建pages_avm_demo 目录
  for (let i = 0; i < pages_avm_demo_file.length; i++) {
    fs.mkdirsSync(`${config.rootPath}/pages-avm-demo/${pages_avm_demo_file[i]}`, function (error) {
      if (error) {
        console.log(error);
      }
      console.log(`${pages_avm_demo_file[i]} 创建成功`);
    })
  }


  // 创建 pages_avm_demo_two 目录
  for (let i = 0; i < pages_avm_demo_two_file.length; i++) {
    fs.mkdirsSync(`${config.rootPath}/pages-avm-demo-two/${pages_avm_demo_two_file[i]}`, function (error) {
      if (error) {
        console.log(error);
      }
      console.log(`${pages_avm_demo_two_file[i]} 创建成功`);
    })
  }

  // 创建 pages_template 目录
  for (let i = 0; i < pages_template_file.length; i++) {
    fs.mkdirsSync(`${config.rootPath}/pages-page-template/${pages_template_file[i]}`, function (error) {
      if (error) {
        console.log(error);
      }
      console.log(`${pages_template_file[i]} 创建成功`);
    })
  }

}

// `[2/5 复制pages的文件到分的各个包中]`);
if (1) {
  console.log(`[2/5 复制pages的文件到分的各个包中]`);
  // 复制pages-api目录
  for (let i = 0; i < pages_api_file.length; i++) {
    copyDir(`${config.oldFile}${pages_api_file[i]}`, `${config.rootPath}/pages-api/${pages_api_file[i]}`, (error) => {
      if (error) {
        console.log(error);
      }
    })
  }


  // 复制pages-avm-demo目录
  for (let i = 0; i < pages_avm_demo_file.length; i++) {
    copyDir(`${config.oldFile}${pages_avm_demo_file[i]}`, `${config.rootPath}/pages-avm-demo/${pages_avm_demo_file[i]}`, (error) => {
      if (error) {
        console.log(error);
      }
    })
  }
  // 复制pages-avm-demo-two目录
  for (let i = 0; i < pages_avm_demo_two_file.length; i++) {
    copyDir(`${config.oldFile}${pages_avm_demo_two_file[i]}`, `${config.rootPath}/pages-avm-demo-two/${pages_avm_demo_two_file[i]}`, (error) => {
      if (error) {
        console.log(error);
      }
    })
  }
  // 复制pages-page-template目录
  for (let i = 0; i < pages_template_file.length; i++) {
    copyDir(`${config.oldFile}${pages_template_file[i]}`, `${config.rootPath}/pages-page-template/${pages_template_file[i]}`, (error) => {
      if (error) {
        console.log(error);
      }
    })
  }

}

// `[3/5 修改微信小程序的app.json内容]`);
if (1) {
  console.log(`[3/5 修改微信小程序的app.json内容]`);
  fs.copy(`./app.json`, `${config.rootPath}/app.json`, (error) => {
    if (error) {
      console.log(error);
    }
  })
}

// `[4/5 删除pages里没用的文件及文件夹 防止微信小程序包过大]`
// 如果你想删除一个文件或目录而不管它是否有内容，只需调用fs.remove(path).
if (1) {
  console.log(`[4/5 删除pages里没用的文件及文件夹 防止微信小程序包过大]`);
  setTimeout(() => {
    for (let i = 0; i < willClearedDir.length; i++) {
      fs.remove(`${config.rootPath}/pages/${willClearedDir[i]}`, error => {
        if (error) {
          console.log(error);
        }
      })
    }

    for (let i = 0; i < pages_api_file.length; i++) {
      fs.remove(`${config.rootPath}/pages/${pages_api_file[i]}`, error => {
        if (error) {
          console.log(error);
        }
      })
    }

    for (let i = 0; i < pages_avm_demo_file.length; i++) {
      fs.remove(`${config.rootPath}/pages/${pages_avm_demo_file[i]}`, error => {
        if (error) {
          console.log(error);
        }
      })
    }

    for (let i = 0; i < pages_avm_demo_two_file.length; i++) {
      fs.remove(`${config.rootPath}/pages/${pages_avm_demo_two_file[i]}`, error => {
        if (error) {
          console.log(error);
        }
      })
    }

    for (let i = 0; i < pages_template_file.length; i++) {
      fs.remove(`${config.rootPath}/pages/${pages_template_file[i]}`, error => {
        if (error) {
          console.log(error);
        }
      })
    }


  }, 100);
}

//5/5 copy base 文件到各个分包的文件夹
if (1) {
  console.log(`[5/5 copy base.js 文件到各个分包的文件夹]`);
  let dirName = [
    'pages-api',
    'pages-avm-demo',
    'pages-avm-demo-two',
    'pages-avm-demo',
    'pages-page-template'
  ]
  for (let i = 0; i < dirName.length; i++) {
    fs.copy(`./base.js`, `${config.rootPath}/${dirName[i]}/base.js`, (error) => {
      if (error) {
        console.log(error);
      }
    })
  }
}


setTimeout(() => {
  // npm cache verify 
}, 101);

/*
 * 复制目录、子目录，及其中的文件
 * @param src {String} 要复制的目录
 * @param dist {String} 复制到目标目录
 */
function copyDir(src, dist, callback) {
  fs.access(dist, function (err) {
    if (err) {
      // 目录不存在时创建目录
      fs.mkdirSync(dist);
    }
    _copy(null, src, dist);
  });

  function _copy(err, src, dist) {
    if (err) {
      callback(err);
    } else {
      fs.readdir(src, function (err, paths) {
        if (err) {
          callback(err)
        } else {
          paths.forEach(function (path) {
            var _src = src + '/' + path;
            var _dist = dist + '/' + path;
            fs.stat(_src, function (err, stat) {
              if (err) {
                callback(err);
              } else {
                // 判断是文件还是目录
                if (stat.isFile()) {
                  fs.writeFileSync(_dist, fs.readFileSync(_src));
                } else if (stat.isDirectory()) {
                  // 当是目录是，递归复制
                  copyDir(_src, _dist, callback)
                }
              }
            })
          })
        }
      })
    }
  }
}



